package com.geekaca.jsp.service;

import com.geekaca.jsp.mapper.BrandMapper;
import com.geekaca.jsp.pojo.Brand;
import com.geekaca.jsp.utils.SqlUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class BrandService {
    public List<Brand> getAllBrands() {
        SqlSession sqlSession = SqlUtil.openSession();
        BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
        List<Brand> brands = brandMapper.selectAllBrands();
        sqlSession.close();
        return brands;
    }

    public List<Brand> getBrandsByPage(Integer page) {

        SqlSession sqlSession = SqlUtil.openSession();
        BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
        int start = (page - 1) * 5;
        int end = 5;
        List<Brand> brands = brandMapper.selectBrandByPageNew(start, end);
        return brands;
    }

    public int addBrand(Brand brand) {
        SqlSession sqlSession = SqlUtil.openSession();
        BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
        int i = brandMapper.insertBrand(brand);
        sqlSession.close();
        return i;
    }

    public int updateBrand(Brand brand) {
        SqlSession sqlSession = SqlUtil.openSession();
        BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
        int i = brandMapper.updateBrand(brand);
        return i;
    }

    public int deleteBrand(Integer id) {
        SqlSession sqlSession = SqlUtil.openSession();
        BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
        int i = brandMapper.deleteBrand(id);
        return i;
    }

}
